package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.CheckInventoryRecordService
import com.zxy.supplier_system.server.utils.GoodsExcelField
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.NullableLocalDateRange
import org.springframework.core.io.InputStreamResource
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime

@RestController
@RequestMapping("checkInventoryRecords")
class CheckInventoryRecordController(
    private val checkInventoryRecordService: CheckInventoryRecordService,
) {

    data class CreateCheckInventoryRecordRequest(
        val name: String?
    )

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PostMapping
    fun createCheckInventoryRecord(request: CreateCheckInventoryRecordRequest):QueryCheckInventoryRecordResponse{
        return this.checkInventoryRecordService.createCheckInventoryRecord(request)
    }

    class QueryCheckInventoryRecordRequest(
        val startDate: LocalDate?,
        val endDate: LocalDate?,
        val employeeIds: List<Int> = emptyList(),
        val submitted: Boolean?,
    )

    @HasAuthority(Authority.卖场_订单_查询)
    @GetMapping
    fun queryCheckInventoryRecord(queryCheckInventoryRecordRequest: QueryCheckInventoryRecordRequest, pageable: Pageable):Page<QueryCheckInventoryRecordResponse> {
        return this.checkInventoryRecordService.queryCheckInventoryRecord(queryCheckInventoryRecordRequest,pageable)
    }

    data class QueryCheckInventoryRecordResponse(
        val id:Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val goodsKindAmount:Int,
        val name:String,
        val submittedDateTime: OffsetDateTime?,
    )

    data class UpdateOrderGoodsRequest(
        val items:List<OrderGoodsItem>
    )

    data class OrderGoodsItem(
        val id:Int,
        val amount:Int
    )

    @HasAuthority(Authority.库存_盘点_盘点)
    @PatchMapping("{id}/goods")
    fun updateOrderGoods(@RequestBody updateOrderGoodsRequest: UpdateOrderGoodsRequest, @PathVariable id: Int,
    ){
        return this.checkInventoryRecordService.updateOrderGoods(id,updateOrderGoodsRequest)
    }

    @HasAuthority(Authority.库存_盘点_查询)
    @GetMapping("{id}")
    fun getCheckInventoryRecordDetail(@PathVariable id: Int, includeItems:Boolean = false):CheckInventoryRecordDetail{
        return this.checkInventoryRecordService.getCheckInventoryRecordDetail(id,includeItems)
    }

    data class CheckInventoryRecordDetail(
        val id:Int,
        val createdDateTime: OffsetDateTime,
        val name:String,
        val createdBy: IntIdAndName,
        val items:List<CheckInventoryRecordItemResponse>,
        val submittedDateTime:OffsetDateTime?,
    )

    data class CheckInventoryRecordItemResponse(
        val id:Int,
        val name: String,
        val barcode:String,
        val price: BigDecimal,
        val amount:Int,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime: OffsetDateTime?,
        val brand:IntIdAndName?,
        val category:IntIdAndName?,
        val amountInOrder:Int,
        val boxSpecification:String?
    )

    @HasAuthority(Authority.库存_盘点_查询)
    @GetMapping("employees", params = ["projection=summary"])
    fun getCheckInventoryRecordCreatedEmployees():List<IntIdAndName>{
        return this.checkInventoryRecordService.getAllEmployees()
    }


    @HasAuthority(Authority.库存_盘点_盘点)
    @PatchMapping("{id}/submit")
    fun submitOrder(@PathVariable id: Int ){
        return this.checkInventoryRecordService.submitCheckInventoryRecord(id)
    }

    @HasAuthority(Authority.库存_盘点_查询)
    @GetMapping("createdDateRanges")
    fun getAllCheckInventoryRecordCreatedDateRange(): NullableLocalDateRange {
        return this.checkInventoryRecordService.getAllCheckInventoryRecordCreatedDateRange()
    }

    class ExportToExcelRequest(
        val fields: Array<GoodsExcelField> = arrayOf(
            GoodsExcelField.条码,
            GoodsExcelField.名称,
            GoodsExcelField.数量,
            GoodsExcelField.单位,
            GoodsExcelField.库存,
            GoodsExcelField.品牌,
            GoodsExcelField.类别,
            GoodsExcelField.厂商,
            GoodsExcelField.箱规,
        )
    )


    @HasAnyAuthority(Authority.库存_盘点_查询)
    @GetMapping("{id}/excel")
    fun exportToExcel(
        request: ExportToExcelRequest,
        @PathVariable id: Int
    ): InputStreamResource {
        return this.checkInventoryRecordService.exportToExcel(id, request.fields)
    }

}
